﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace NumberPuzzle
{
    public partial class Main : Form
    {
        #region Declarations

        private int[] numberOfMovements = new int[1];

        #endregion Declarations

        public Main()
        {
            InitializeComponent();
        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int[,] x = new int[3, 3];
            x[0, 0] = 1;
            x[0, 1] = 2;
            x[0, 2] = 3;
            x[1, 0] = 4;
            x[1, 1] = 5;
            x[1, 2] = 6;
            x[2, 0] = 7;
            x[2, 1] = 8;
            x[2, 2] = 9;

            this.boardNumber.Initialize(x);
        }

        private void disorderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            numberOfMovements[0] = 0;
            DisorderForm disOrderForm = new DisorderForm(numberOfMovements);
            disOrderForm.ShowDialog();

            this.boardNumber.DisOrderPuzzle(Convert.ToInt32(numberOfMovements[0]));
        }

        private void autoresToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AuthorsForm authors = new AuthorsForm();
            authors.ShowDialog();
        }

        private void btnResolver_Click(object sender, EventArgs e)
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            List<int> movesToSolve = PuzzleSolver.SolvePuzzle(this.boardNumber.puzzle);

            stopWatch.Stop();
            // Format and display the TimeSpan value. 
            TimeSpan ts = stopWatch.Elapsed;
            string elapsedTime = String.Format("{0:00}.{1:000}", ts.Seconds, ts.Milliseconds);
            this.boardNumber.Update();

            lblNodos.Text = Convert.ToString(PuzzleSolver.nodeCount);
            lblMovimientos.Text = Convert.ToString(movesToSolve.Count/2);
            lblTiempo.Text = elapsedTime + " seg";

            for (int i = 0; i < movesToSolve.Count; i += 2)
            {
                this.boardNumber.MovePiece(movesToSolve[i], movesToSolve[i + 1], true);
                this.boardNumber.Update();
                Thread.Sleep(500);
            }
        }
    }
}
